/*
 * Copyright (c) 2012 RTCLab
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this list of
 *    conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
 *    of conditions and the following disclaimer in the documentation and/or other materials
 *    provided with the distribution.
 *
 */

package com.rtckit {

public class URLParser {
	private var reg:RegExp = /(?P<protocol>[a-zA-Z]+) : \/\/  (?P<host>[^:\/]*) (:(?P<port>\d+))?  ((?P<path>[^?]*))? ((?P<parameters>.*))? /x;
    public var url:String;

    public var host:String = "";
    public var port:String = "";
    public var protocol:String = "";
    public var path:String = "";
    public var parameters:Object = [];

	public function URLParser(url:String) {
		this.url = url;
		var results:Array = reg.exec(url);
		protocol = results.protocol
		host = results.host;
		port = results.port;
		path = results.path;
		var paramsStr:String = results.parameters;
		if (paramsStr != "") {
			parameters = null;
			parameters = new Object();
			if (paramsStr.charAt(0) == "?") {
				paramsStr = paramsStr.substring(1);
			}
			var params:Array = paramsStr.split("&");
			for each(var paramStr:String in params) {
				var param:Array = paramStr.split("=");
				parameters[param[0]] = param[1];
			}
		}
    }
}

}